package blueBridge;

import java.util.Scanner;

public class UnionArea {
    
    static Scanner scan = new Scanner(System.in);
    
    int n;
    Map a, b;
    
    
    private UnionArea(int n, boolean[][] a, boolean[][] b) {
        super();
        this.n = n;
        this.a = new Map(a);
        this.b = new Map(b);
    }
    
    private int result() {
        int maxSoleArea = a.maxSoleArea > b.maxSoleArea ? a.maxSoleArea : b.maxSoleArea;
        int maxUnionArea = a.maxAreaToUnion + b.maxAreaToUnion;
        int maxArea = maxSoleArea > maxUnionArea ? maxSoleArea : maxUnionArea;
        return maxArea;
    }
    
    public static void main(String[] args) {
        int n = scan.nextInt();
        boolean[][] a = new boolean[n][n];
        boolean[][] b = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (scan.nextInt() == 1) {
                    a[i][j] = true;
                    
                }
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (scan.nextInt() == 1) {
                    b[i][j] = true;
                }
            }
        }
        UnionArea m = new UnionArea(n, a, b);
        System.out.println(m.result());
    }

}
class Map {
    int n;
    boolean[][] map;
    boolean[][] visited;
    int maxAreaToUnion;
    int maxSoleArea;
    int tempArea;
    
    public Map(boolean[][] map) {
        this.map = map;
        n = map.length;
        visited = new boolean[n][n];
        setArea();
    }
    
    // 遍历所有，找到不拼接的最大面积，遍历四个边缘，找到与边缘接壤的最大区域，并返回面积，
    private void setArea() {
        int n = map.length;
        visited = new boolean[n][n];
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                if (map[i][j] && !visited[i][j]) {
                    tempArea = 0;
                    dfs(i, j);
                    System.out.println(tempArea);
                    if (tempArea > maxSoleArea) {
                        maxSoleArea = tempArea;
                        if (i == 0 || j == 0 || i == n-1 || j == n-1) {
                            maxAreaToUnion = tempArea;
                        }
                    }
                }
            }
        }
    }
    
    private void dfs(int i, int j) {
        System.out.println(i + ", " + j);
        int n = map.length;
        // 下
        if (i > 0 && map[i - 1][j] && !visited[i - 1][j]) {
            visited[i - 1][j] = true;
            tempArea++;
            dfs(i - 1, j);
        }
        // 上
        if (i < n - 1 && map[i + 1][j] && !visited[i + 1][j]) {
            visited[i + 1][j] = true;
            tempArea++;
            dfs(i + 1, j);
        }
        // 左
        if (j > 0 && map[i][j - 1] && !visited[i][j - 1]) {
            visited[i][j - 1] = true;
            tempArea++;
            dfs(i, j - 1);
        }
        // 右
        if (j < n - 1 && map[i][j + 1] && !visited[i][j + 1]) {
            visited[i][j + 1] = true;
            tempArea++;
            dfs(i, j + 1);
        }

    }

}
